package algocasts;


public class P5_getSum {
    public int getSumRecursive(int a, int b) {
        return b == 0 ? a : getSumRecursive(a ^ b, (a & b) << 1);
        // 如果b为0直接返回a，b不为零则计算
    }

    public int getSumIterative(int a, int b) {
        while (b != 0) {
            int sum = a ^ b;    // 计算无进位和
            int carry = (a & b) << 1;   // 计算进位
            a = sum;    // 无进位和赋值给a
            b = carry;  // 将进位赋值为b
        }
        return a;
    }

}
